<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div>
    <header class="home-header wrap" :class="{'active' : state.headerScroll}">
      <div class="search-container">
        <div class="input-container">
          <input type="text" class="search-input" v-model="state.keyword" placeholder="a13 号电池">
        </div>
        <div class="search-button" @click="getSearch">
          搜索
        </div>
      </div>
    </header>
    <div class="scrollable-content">
      <nav-bar/>
      <swiper :list="state.swiperList"></swiper>
      <!-- <div class="category-list">
        <div v-for="item in state.categoryList" v-bind:key="item.categoryId" @click="tips">
          <img :src="item.imgUrl">
          <span>{{ item.name }}</span>
        </div>
      </div> -->

      <div class="product-list-wrap">
        <!--      <div class="product-list-content">-->
        <!--&lt;!&ndash;        <header class="category-header wrap">&ndash;&gt;-->
        <!--&lt;!&ndash;          <i class="nbicon nbfanhui" @click="goBack"></i>&ndash;&gt;-->
        <!--&lt;!&ndash;          <div class="header-search">&ndash;&gt;-->
        <!--&lt;!&ndash;            <i class="nbicon nbSearch"></i>&ndash;&gt;-->
        <!--&lt;!&ndash;            <input&ndash;&gt;-->
        <!--&lt;!&ndash;                type="text"&ndash;&gt;-->
        <!--&lt;!&ndash;                class="search-title"&ndash;&gt;-->
        <!--&lt;!&ndash;                v-model="state.keyword"/>&ndash;&gt;-->
        <!--&lt;!&ndash;          </div>&ndash;&gt;-->
        <!--&lt;!&ndash;          <span class="search-btn" @click="getSearch">搜索</span>&ndash;&gt;-->
        <!--&lt;!&ndash;        </header>&ndash;&gt;-->
        <!--&lt;!&ndash;        <van-tabs type="card" color="#1baeae" @click-tab="changeTab">&ndash;&gt;-->
        <!--&lt;!&ndash;          <van-tab title="推荐" name=""></van-tab>&ndash;&gt;-->
        <!--&lt;!&ndash;          <van-tab title="新品" name="new"></van-tab>&ndash;&gt;-->
        <!--&lt;!&ndash;          <van-tab title="价格" name="price"></van-tab>&ndash;&gt;-->
        <!--&lt;!&ndash;        </van-tabs>&ndash;&gt;-->
        <!--      </div>-->
        <div class="content">
          <van-pull-refresh v-model="state.refreshing" @refresh="onRefresh" class="product-list-refresh">
            <van-list
                v-model:loading="state.loading"
                :finished="state.finished"
                :finished-text="state.productList.length ? '没有更多了' : '搜索想要的商品'"
                @load="onLoad"
                :immediate-check="false"
            >
              <!-- <p v-for="item in list" :key="item">{{ item }}</p> -->
              <template v-if="state.productList.length">
                <div class="product-item" v-for="(item, index) in state.productList" :key="index"
                     @click="productDetail(item)">
                  <img :src="$filters.prefix(item.goodsCoverImg)"/>
                  <div class="product-info">
                    <p class="name">{{ item.goodsName }}</p>
                    <p class="subtitle">{{ item.goodsIntro }}</p>
                    <span class="price">￥ {{ item.sellingPrice }}</span>
                  </div>
                </div>
              </template>
              <img class="empty" v-else src="https://s.yezgea02.com/1604041313083/kesrtd.png" alt="搜索">
            </van-list>
          </van-pull-refresh>
        </div>
      </div>
    </div>

    <!--    <div class="good">-->
    <!--      <header class="good-header">新品上线</header>-->
    <!--      <van-skeleton title :row="3" :loading="state.loading">-->
    <!--        <div class="good-box">-->
    <!--          <div class="good-item" v-for="item in state.newGoodses" :key="item.goodsId" @click="goToDetail(item)">-->
    <!--            <img :src="$filters.prefix(item.goodsCoverImg)" alt="">-->
    <!--            <div class="good-desc">-->
    <!--              <div class="title">{{ item.goodsName }}</div>-->
    <!--              <div class="price">¥ {{ item.sellingPrice }}</div>-->
    <!--            </div>-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </van-skeleton>-->
    <!--    </div>-->
    <!--    <div class="good">-->
    <!--      <header class="good-header">热门商品</header>-->
    <!--      <van-skeleton title :row="3" :loading="state.loading">-->
    <!--        <div class="good-box">-->
    <!--          <div class="good-item" v-for="item in state.hots" :key="item.goodsId" @click="goToDetail(item)">-->
    <!--            <img :src="$filters.prefix(item.goodsCoverImg)" alt="">-->
    <!--            <div class="good-desc">-->
    <!--              <div class="title">{{ item.goodsName }}</div>-->
    <!--              <div class="price">¥ {{ item.sellingPrice }}</div>-->
    <!--            </div>-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </van-skeleton>-->
    <!--    </div>-->
    <!--    <div class="good" :style="{ paddingBottom: '100px'}">-->
    <!--      <header class="good-header">最新推荐</header>-->
    <!--      <van-skeleton title :row="3" :loading="state.loading">-->
    <!--        <div class="good-box">-->
    <!--          <div class="good-item" v-for="item in state.recommends" :key="item.goodsId" @click="goToDetail(item)">-->
    <!--            <img :src="$filters.prefix(item.goodsCoverImg)" alt="">-->
    <!--            <div class="good-desc">-->
    <!--              <div class="title">{{ item.goodsName }}</div>-->
    <!--              <div class="price">¥ {{ item.sellingPrice }}</div>-->
    <!--            </div>-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </van-skeleton>-->
    <!--    </div>-->
  </div>
</template>

<script setup>
import {reactive, onMounted, nextTick} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import swiper from '@/components/Swiper.vue'
import navBar from '@/components/NavBar.vue'
import {getHome} from '@/service/home'
import {getLocal} from '@/common/js/utils'
import {showLoadingToast, closeToast, showToast} from 'vant'
import {useCartStore} from '@/stores/cart'
import {search} from "@/service/good";

const route = useRoute()
const init = async () => {
  // if (!state.keyword) {
  //   state.finished = true
  //   state.loading = false;
  //   return
  // }
  const {data, data: {list}} = await search({
    pageNumber: state.page,
    keyword: state.keyword
  })

  state.productList = state.productList.concat(list)
  state.totalPage = data.totalPage
  state.loading = false;
  if (state.page >= data.totalPage) state.finished = true
}

const productDetail = (item) => {
  router.push({path: `/product/${item.goodsId}`})
}

const getSearch = () => {
  if (state.keyword.trim()) {
    router.push({path: '/product-list', query: {keyword: state.keyword}})
  } else {
    showToast('请输入搜索关键词')
  }
}

const onLoad = () => {
  if (!state.refreshing && state.page < state.totalPage) {
    state.page = state.page + 1
  }
  if (state.refreshing) {
    state.productList = [];
    state.refreshing = false;
  }
  init()
}

const onRefresh = () => {
  state.refreshing = true
  state.finished = false
  state.loading = true
  state.page = 1
  onLoad()
}

const changeTab = ({name}) => {
  console.log('name', name)
  state.orderBy = name
  onRefresh()
}

const cart = useCartStore()
const router = useRouter()
const state = reactive({
  keyword: route.query.keyword || '',
  searchBtn: false,
  seclectActive: false,
  refreshing: false,
  list: [],
  loading: false,
  finished: false,
  productList: [],
  totalPage: 0,
  page: 1,
  orderBy: '',
  swiperList: [], // 轮播图列表
  isLogin: false, // 是否已登录
  headerScroll: false, // 滚动透明判断
  hots: [],
  newGoodses: [],
  recommends: [],
  categoryList: [
    {
      name: '新蜂超市',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E8%B6%85%E5%B8%82%402x.png',
      categoryId: 100001
    }, {
      name: '新蜂服饰',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E6%9C%8D%E9%A5%B0%402x.png',
      categoryId: 100003
    }, {
      name: '全球购',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%A8%E7%90%83%E8%B4%AD%402x.png',
      categoryId: 100002
    }, {
      name: '新蜂生鲜',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E7%94%9F%E9%B2%9C%402x.png',
      categoryId: 100004
    }, {
      name: '新蜂到家',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%88%B0%E5%AE%B6%402x.png',
      categoryId: 100005
    }, {
      name: '充值缴费',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%85%E5%80%BC%402x.png',
      categoryId: 100006
    }, {
      name: '9.9元拼',
      imgUrl: 'https://s.yezgea02.com/1604041127880/9.9%402x.png',
      categoryId: 100007
    }, {
      name: '领劵',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E9%A2%86%E5%88%B8%402x.png',
      categoryId: 100008
    }, {
      name: '省钱',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E7%9C%81%E9%92%B1%402x.png',
      categoryId: 100009
    }, {
      name: '全部',
      imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%A8%E9%83%A8%402x.png',
      categoryId: 100010
    }
  ]
})
onMounted(async () => {
  // const token = getLocal('token')
  // if (token) {
  //   state.isLogin = true
  //   // 获取购物车数据.
  //   // cart.updateCart()
  // }
  showLoadingToast({
    message: '加载中...',
    forbidClick: true
  });
  const {data} = await getHome()
  state.swiperList = data.carousels
  state.newGoodses = data.newGoodses
  state.hots = data.hotGoodses
  state.recommends = data.recommendGoodses
  state.loading = false
  
  // 初始化商品列表
  onLoad()
  
  closeToast()
})

nextTick(() => {
  document.body.addEventListener('scroll', () => {
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
    scrollTop > 100 ? state.headerScroll = true : state.headerScroll = false
  })
})
const tips = () => {
  showToast('敬请期待');
}

const openCamera = () => {
  showToast('相机功能敬请期待');
}
</script>

<style lang="less" scoped>
@import '../common/style/mixin';

.scrollable-content {
  padding-top: 60px;
  padding-bottom: 50px; /* 为底部导航栏留出空间 */
  width: 100%;
  box-sizing: border-box;
  overflow-y: auto;
  height: 100vh;
}

.home-header {
  position: fixed;
  left: 0;
  top: 0;
  .wh(100%, 60px);
  line-height: 50px;
  padding: 10px 15px;
  .boxSizing();
  font-size: 15px;
  background-color: #FF6127;
  color: #fff;
  z-index: 10000;

  .search-container {
    display: flex;
    width: 100%;
    height: 40px;
    align-items: center;
    justify-content: space-between;
  }

  .input-container {
    display: flex;
    flex: 1;
    height: 40px;
    background-color: #fff;
    border-radius: 20px;
    align-items: center;
    padding: 0 15px;
    margin-right: 10px;
    position: relative;
  }

  .search-input {
    flex: 1;
    height: 30px;
    border: none;
    outline: none;
    font-size: 14px;
    color: #333;
    background-color: transparent;
  }

  .camera-icon {
    display: none; /* 隐藏相机图标，因为图片中没有显示 */
  }

  .search-button {
    min-width: 60px;
    height: 40px;
    background-color: #fff;
    color: #FF6127;
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    padding: 0 15px;
  }

  .nbmenu2 {
    color: @primary;
  }

  &.active {
    background: #FF6127;

    .nbmenu2 {
      color: #fff;
    }

    .login {
      color: #fff;
    }
  }

  .icon-iconyonghu {
    color: #fff;
    font-size: 22px;
  }

  .login {
    color: @primary;
    line-height: 52px;

    .van-icon-manager-o {
      font-size: 20px;
      vertical-align: -3px;
    }
  }
}

.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 13px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      .wh(36px, 36px);
      margin: 13px auto 8px auto;
    }
  }
}

.good {
  .good-header {
    background: #f9f9f9;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: @primary;
    font-size: 16px;
    font-weight: 500;
  }

  .good-box {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;

    .good-item {
      box-sizing: border-box;
      width: 50%;
      border-bottom: 1PX solid #e9e9e9;
      padding: 10px 10px;

      img {
        display: block;
        width: 120px;
        margin: 0 auto;
      }

      .good-desc {
        text-align: center;
        font-size: 14px;
        padding: 10px 0;

        .title {
          color: #222333;
        }

        .price {
          color: @primary;
        }
      }

      &:nth-child(2n + 1) {
        border-right: 1PX solid #e9e9e9;
      }
    }
  }
}

.floor-list {
  width: 100%;
  padding-bottom: 50px;

  .floor-head {
    width: 100%;
    height: 40px;
    background: #F6F6F6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    .boxSizing();

    .floor-category {
      width: 50%;
      padding: 10px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      .boxSizing();

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 17px;
        color: #333;

        &:nth-child(2) {
          padding: 5px 0;
          font-size: 13px;
          color: @primary;
        }
      }

      .floor-products {
        .fj();
        width: 100%;

        img {
          .wh(65px, 65px);
        }
      }
    }
  }
}

.product-list-content {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 1000;
  background: #fff;

  .category-header {
    .fj();
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    .boxSizing();
    font-size: 15px;
    color: #656771;
    z-index: 10000;

    &.active {
      background: @primary;
    }

    .icon-left {
      font-size: 25px;
      font-weight: bold;
    }

    .header-search {
      display: flex;
      width: 76%;
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: #F7F7F7;
      .borderRadius(20px);

      .nbSearch {
        padding: 0 5px 0 20px;
        font-size: 17px;
      }

      .search-title {
        font-size: 12px;
        color: #666;
        background: #F7F7F7;
      }
    }

    .icon-More {
      font-size: 20px;
    }

    .search-btn {
      height: 28px;
      margin: 8px 0;
      line-height: 28px;
      padding: 0 5px;
      color: #fff;
      background: @primary;
      .borderRadius(5px);
      margin-top: 10px;
    }
  }
}

.content {
  width: 100%;
}

.product-list-refresh {
  width: 100%;
  
  .product-item {
    .fj();
    margin: 10px 0;
    width: 100%;
    height: auto;
    min-height: 120px;
    padding: 15px 0;
    border-bottom: 1px solid #dcdcdc;

    img {
      width: 120px;
      height: 120px;
      object-fit: cover;
      padding: 0 10px;
      .boxSizing();
    }

    .product-info {
      width: 56%;
      height: auto;
      min-height: 100px;
      padding: 5px;
      text-align: left;
      .boxSizing();
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      p {
        margin: 0
      }

      .name {
        width: 100%;
        max-height: 40px;
        line-height: 20px;
        font-size: 15px;
        color: #333;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .subtitle {
        width: 100%;
        max-height: 50px;
        padding: 5px 0;
        line-height: 25px;
        font-size: 13px;
        color: #999;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      .price {
        color: @primary;
        font-size: 16px;
      }
    }
  }

  .empty {
    display: block;
    width: 150px;
    margin: 50px auto 20px;
  }
}
</style>